package main

import (
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"iron_man_comm/src/goroslib"
	"iron_man_comm/src/goroslib/dns"
	"iron_man_comm/src/msg/common_msg"
	"iron_man_comm/src/tools/adb_read"
	"iron_man_comm/src/tools/log"
	"os"
	"time"
)

type SocVersion struct {
	Date    string `json:"date"`
	Version string `json:"version"`
}

func readSocVersion(ip string) (string, error) {
	content, err := adb_read.ReadFile(ip, "/root/install/version.txt")
	if err != nil {
		return "", err
	}
	var socVersion SocVersion
	err = json.Unmarshal([]byte(content), &socVersion)
	if err != nil {
		return "", err
	}
	os.Remove("version.txt")
	return socVersion.Version, nil
}

func readMcuVersion(node *goroslib.Node) (string, error) {
	version := ""
	subscriber, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
		Node:  node,
		Topic: "/serial_com_driver/software_version",
		Callback: func(msg *common_msg.SoftwareVersion) {
			version = msg.SoftwareVersion
		},
		QueueSize: 2,
	})
	if err != nil {
		return "", err
	}
	defer subscriber.Close()

	//最多等5s
	curr := time.Now().UnixMilli()
	for time.Now().UnixMilli()-curr < 5000 {
		if len(version) != 0 {
			break
		}
	}
	if len(version) == 0 {
		return "", errors.New("read mcu version timeout")
	}
	return version, nil
}

func readBaseStationVersion(node *goroslib.Node) (string, error) {
	version := ""
	subscriber, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
		Node:  node,
		Topic: "/base/version/info",
		Callback: func(msg *common_msg.VersionInfo) {
			version = fmt.Sprintf("%d.%d.%d", msg.MainVersion, msg.FeatureVersion, msg.FixVersion)
		},
		QueueSize: 2,
	})
	if err != nil {
		return "", err
	}
	defer subscriber.Close()

	//最多等5s
	curr := time.Now().UnixMilli()
	for time.Now().UnixMilli()-curr < 5000 {
		if len(version) != 0 {
			break
		}
	}
	if len(version) == 0 {
		return "", errors.New("read base station version timeout")
	}
	return version, nil
}

func main() {
	log.InitializeLog()

	ip := flag.String("ip", "127.0.0.1", "目标ip")
	flag.Parse()
	//soc
	socVersion, err := readSocVersion(*ip)
	if err != nil {
		log.Error.Printf("read soc version fail, err: %s", err.Error())
		return
	}
	log.Info.Printf("soc version: %s", socVersion)

	dns.GetDNSCacheInterface().UpdateDNSMapping("rk3562-buildroot", *ip)
	node, err := goroslib.NewNode(goroslib.NodeConf{
		MasterAddress: fmt.Sprintf("%s:11311", *ip),
		Name:          fmt.Sprintf("temp_node_%", time.Now().UnixMilli()),
	})
	if err != nil {
		log.Error.Printf("create node fail, err: %s", err.Error())
		return
	}
	defer node.Close()

	//mcu
	mcuVersion, err := readMcuVersion(node)
	if err != nil {
		log.Error.Printf("read mcu version fail, err: %s", err.Error())
		return
	}
	log.Info.Printf("mcu version: %s", mcuVersion)

	//base station
	baseStationVersion, err := readBaseStationVersion(node)
	if err != nil {
		log.Error.Printf("read base station version fail, err: %s", err.Error())
		return
	}
	log.Info.Printf("base station version: %s", baseStationVersion)
}
